/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package brisollogic;

import jBCrypt.BCrypt;

/**
 *
 * @author Alex
 */
public class UserCredentials implements Credentials {
    private UserCredentials(String password, String email){
        this.password = password;
        this.email = email;
    }    
    private final String password;
    private final String email;
    
    public static UserCredentials getInstanceHashPassword(String email, String password) {
        validate(password, email);
        password = hashPassword(password);
        return new UserCredentials(password, email);
    }
    
    public static UserCredentials getInstance(String email, String password) {
        validate(password, email);        
        return new UserCredentials(password, email);
    }
    
    @Override 
    public String getPassword() {
        return password;
    }
    
    @Override
    public String getEmail() {
        return email;
    }    
    
    private static String hashPassword(String password) {
        return BCrypt.hashpw(password, BCrypt.gensalt());
    }
    
    private static void validate(String password, String email) {
        if(password == null) {
            throw new NullPointerException("String password must not be null");
        } else if(email == null) {
            throw new NullPointerException("String email must not be null");
        } else if(email.length() <= 6) {
            throw new IllegalArgumentException("Email must be aleast 6 characters");
        } else if(password.length() <= 6) {
            throw new IllegalArgumentException("Password must be aleast 6 characters");
        }
    }
    
    @Override
    public String toString() {
        return ("Email " + email + " Password " + password);
    }
}
